<?php
	require_once("settings.php");
	class Database{
		private $mysqli = NULL;
		
		public function Connect(Settings $DBconfig){
			$this->mysqli = new MySQLi($DBconfig->m_host, $DBconfig->m_user, 
										$DBconfig->m_password, $DBconfig->m_db);
			if($this->mysqli->connect_error){
				throw new Exception($this->mysqli->connect_error);	
			}
			$this->mysqli->set_charset("utf-8");
			return TRUE;
		}
		public function Close(){
			return $this->mysqli->close();
		}
		public function AuthUser($sqlQuery){
			$set = new Settings();
			$ret = 0;
			//Skapar Databasuppkoppling
			$this->Connect($set);
			//Förbereder SQL-sats
			$stmt = $this->mysqli->prepare($sqlQuery);
			//Kontrollerar så det inte förekommer någon SQL-injection
			if($stmt == FALSE){
				throw new Exception($this->mysqli->error);	
			}
			//Kör SQL-satsen
			if($stmt->execute() == FALSE){
				throw new Exception($this->mysqli->error);	
			}
			//Talar om var värdet ska hamna
			if($stmt->bind_result($ret) == FALSE){
				throw new Exception($this->mysqli->error);
			}
			//Utför min SQL-sats och avslutar SQL-satsen
			$stmt->fetch();
			$stmt->close();
			//Stänger Databasuppkopplingen
			$this->Close();
			//retunerar användarID:t
			return $ret;	
		}
		public static function Test(Settings $DBconfig){
			$db = new Database();
			if($db->Connect($DBconfig) == FALSE){
				echo "Connect misslyckades";
				return FALSE;
			}
			if($db->Close($DBconfig) == FALSE){
				echo "Close misslyckades";
				return FALSE;	
			}
			return TRUE;
		}
	}
?>